﻿/*
 * ASP.NET Validation Controls for MVC (using WebUIValidation.js)
 *
 * Copyright (c) 2009 Guramrit Singh Dhillon
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

using System;
using System.Web.Mvc;
using Classic.Mvc.Web.UI.Validators;

namespace Classic.Mvc.Web.UI
{
    public static class HtmlHelperExtensions
    {
        public static string ValidationSummary(this HtmlHelper helper, string id, Action<ValidationSummary> props)
        {
            return helper.ValidationSummary(id, props, null);
        }

        public static string ValidationSummary(this HtmlHelper helper, string id,
                                               Action<ValidationSummary> props, object htmlAttributes)
        {
            var validationSummary = new ValidationSummary(id, htmlAttributes);
            props(validationSummary);
            ValidatorScriptManager.Current.AddValidationControl(validationSummary);
            return ((IMvcControl) validationSummary).GetControlText();
        }

        public static string RequiredFieldValidator(this HtmlHelper helper, string id, string controlToValidate,
                                                    Action<RequiredFieldValidator> props)
        {
            return RequiredFieldValidator(helper, id, controlToValidate, props, null);
        }

        public static string RequiredFieldValidator(this HtmlHelper helper, string id, string controlToValidate,
                                                    Action<RequiredFieldValidator> props, object htmlAttributes)
        {
            var validator = new RequiredFieldValidator(id, controlToValidate, htmlAttributes);
            props(validator);
            ValidatorScriptManager.Current.AddValidationControl(validator);
            return ((IMvcControl) validator).GetControlText();
        }

        public static string RangeValidator(this HtmlHelper helper, string id, string controlToValidate,
                                            Action<RangeValidator> props)
        {
            return helper.RangeValidator(id, controlToValidate, props, null);
        }

        public static string RangeValidator(this HtmlHelper helper, string id, string controlToValidate,
                                            Action<RangeValidator> props, object htmlAttributes)
        {
            var validator = new RangeValidator(id, controlToValidate, htmlAttributes);
            props(validator);
            ValidatorScriptManager.Current.AddValidationControl(validator);
            return ((IMvcControl) validator).GetControlText();
        }

        public static string CompareValidator(this HtmlHelper helper, string id, string controlToValidate,
                                              Action<CompareValidator> props, object htmlAttributes)
        {
            var validator = new CompareValidator(id, controlToValidate, htmlAttributes);
            props(validator);
            ValidatorScriptManager.Current.AddValidationControl(validator);
            return ((IMvcControl) validator).GetControlText();
        }

        public static string CompareValidator(this HtmlHelper helper, string id, string controlToValidate,
                                              Action<CompareValidator> props)
        {
            return helper.CompareValidator(id, controlToValidate, props, null);
        }

        public static string RegularExpressionValidator(this HtmlHelper helper, string id, string controlToValidate,
                                                        Action<RegularExpressionValidator> props)
        {
            return helper.RegularExpressionValidator(id, controlToValidate, props, null);
        }

        public static string RegularExpressionValidator(this HtmlHelper helper, string id, string controlToValidate,
                                                        Action<RegularExpressionValidator> props, object htmlAttributes)
        {
            var validator = new RegularExpressionValidator(id, controlToValidate, htmlAttributes);
            props(validator);
            ValidatorScriptManager.Current.AddValidationControl(validator);
            return ((IMvcControl) validator).GetControlText();
        }

        public static string CustomValidator(this HtmlHelper helper, string id, string controlToValidate,
                                             Action<CustomValidator> props)
        {
            return helper.CustomValidator(id, controlToValidate, props, null);
        }

        public static string CustomValidator(this HtmlHelper helper, string id, string controlToValidate,
                                             Action<CustomValidator> props, object htmlAttributes)
        {
            var validator = new CustomValidator(id, controlToValidate, htmlAttributes);
            props(validator);
            ValidatorScriptManager.Current.AddValidationControl(validator);
            return ((IMvcControl) validator).GetControlText();
        }
    }
}